package com.qf.smsplatform.gateway.thread;

import com.qf.smsplatform.common.constants.ReportStateConstants;
import com.qf.smsplatform.common.model.StandardReport;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.gateway.mq.DelayMessagePostProcessor;
import com.qf.smsplatform.gateway.mq.StatQueue;
import com.qf.smsplatform.gateway.netty4.entity.CmppDeliver;
import com.qf.smsplatform.gateway.netty4.entity.ReportMatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;

import static com.qf.smsplatform.gateway.config.RabbitConfig.QUEUE_DELAY_PER_MESSAGE_TTL_MSG_SMS_SEND;

public class SendReportThread implements Runnable{
    private final static Logger log = LoggerFactory.getLogger(SendReportThread.class);

    private AmqpTemplate rabbitTemplate;

    private String pushTopic;
    private String updateTopic;

    private StatQueue statqueue = StatQueue.getInstance();

    public SendReportThread(String pushTopic,String updateTopic,AmqpTemplate rabbitTemplate){
        this.pushTopic = pushTopic;
        this.updateTopic = updateTopic;
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (statqueue.size() > 0) {
                    CmppDeliver deliver = (CmppDeliver) statqueue.remove();  // 从队列中获取到运营商的第二次响应
                    String stat = deliver.getStat();    // 如果返回的是DELIVRD,代表发送成功,其余信息会返回其他值...
                    long msgid = deliver.getMsg_Id_DELIVRD();   //  第一次回调时,响应的msgid2
                    ReportMatch reportMatch = ReportMatch.getInstance();  // 获取临时存储数据的窗口
                    StandardSubmit submit = (StandardSubmit) reportMatch.remove(msgid);  // 基于msgid,获取到存储到窗口中的submit
                    StandardReport report = new StandardReport();
                    if (!stat.equals("DELIVRD")){   //如果stat不是DELIVRD,说明短信发送失败!!
                        report.setState(ReportStateConstants.FAIL);
                        report.setErrorCode(stat);                // 封装report,
                        // TODO 如果发送失败,要将submit中的fee预扣除的费用,添加回去.
                    }else{
                        report.setState(ReportStateConstants.SUCCESS);  //如果stat是DELIVRD,说明短信发送成功!!
                        // TODO 写到ZSet.限流操作.....
                    }
                    report.setSrcID(submit.getSrcSequenceId());  // 封装report, srcID
                    report.setMobile(submit.getDestMobile());    // 封装report, 手机号
                    report.setMsgId(String.valueOf(msgid));      // 封装report, msgId
                    report.setClientID(submit.getClientID());    // 封装report, ClientID
                    report.setSequenceId(submit.getSequenceId());// 封装report, sequenceId

                    // 给客户端推送状态报告的
                    rabbitTemplate.convertAndSend(pushTopic, report);
                    // 修改写到ES中的日志信息,通过死信队列实现了延迟的效果
                    rabbitTemplate.convertAndSend(QUEUE_DELAY_PER_MESSAGE_TTL_MSG_SMS_SEND, report, new DelayMessagePostProcessor(10 * 1000));
                } else {
                    sleep(2000);
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
                sleep(2000);
            }
        }
    }

    private void sleep(long time){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
}
